﻿using NovoSerDes.Funcs;
using NovoSerDesEva;
using NovoUI.UserCtrl;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NovoSerDes.Forms
{
    public partial class Script_Form : UIPage
    {
        private TabControl scriptTC;
        private SerDesComm serDesComm;
        private AddressMeditor meditorA;
        public AddressMeditor MdEditorA
        {
            get { return meditorA; }
            set { meditorA = value; }
        }

        private USBDeviceMeditor usbDeviceMeditor;

        public USBDeviceMeditor USBDeviceMeditor
        {
            get { return usbDeviceMeditor; }
            set { usbDeviceMeditor = value; }
        }
        public Script_Form()
        {
            serDesComm = MainForm.GetSetDesComm;
            if (serDesComm != null)
            {
                if (!serDesComm.IsOpen)
                {
                    UIMessageDialog.ShowWarningDialog(FindForm(), "CH347通道未打开!!!");
                }
                else
                {                 
                    InitializeComponent();
                    TabControl scriptTC = new UITabControl();
                    scriptTC.Alignment = TabAlignment.Top;
                    this.Controls.Add(scriptTC);
                    scriptTC.Dock = DockStyle.Fill;
                    if (scriptTC.TabPages.Count == 0)
                    {
                        int maxScriptWinCnt = 5;

                        //scriptTC.Appearance = TabAppearance.FlatButtons;
                        //scriptTC.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;

                        for (int ix = 0; ix < maxScriptWinCnt; ix++)
                        {
                            FormScript frm_script = new FormScript(serDesComm);
                            scriptTC.TabPages.Add("Script" + ix.ToString());
                            scriptTC.TabPages[ix].Controls.Add(frm_script);
                            frm_script.Dock = DockStyle.Fill;
                            frm_script.BorderStyle = BorderStyle.Fixed3D;
                        }

                        //scriptTC.Alignment = TabAlignment.Bottom;
                    }
                }
                
            }
            else
            {
                UIMessageDialog.ShowWarningDialog(FindForm(), "未选择CH347通道!!!");
            }

        }

        public Script_Form(string name)
        {
            serDesComm = InitSer(name);
            if (serDesComm != null)
            {
                if (!serDesComm.IsOpen)
                {
                    UIMessageDialog.ShowWarningDialog(FindForm(), "CH347通道未打开!!!");
                }
                else
                {
                    InitializeComponent();
                    TabControl scriptTC = new UITabControl();
                    scriptTC.Alignment = TabAlignment.Top;
                    this.Controls.Add(scriptTC);
                    scriptTC.Dock = DockStyle.Fill;
                    if (scriptTC.TabPages.Count == 0)
                    {
                        int maxScriptWinCnt = 5;

                        //scriptTC.Appearance = TabAppearance.FlatButtons;
                        //scriptTC.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;

                        for (int ix = 0; ix < maxScriptWinCnt; ix++)
                        {
                            FormScript frm_script = new FormScript(serDesComm);
                            scriptTC.TabPages.Add("Script" + ix.ToString());
                            scriptTC.TabPages[ix].Controls.Add(frm_script);
                            frm_script.Dock = DockStyle.Fill;
                            frm_script.BorderStyle = BorderStyle.Fixed3D;
                        }

                        //scriptTC.Alignment = TabAlignment.Bottom;
                    }
                }

            }
            else
            {
                UIMessageDialog.ShowWarningDialog(FindForm(), "未选择CH347通道!!!");
            }

        }
        public Script_Form(string name,AddressMeditor meditor)
        {
            serDesComm = InitSer(name);
            if (serDesComm != null)
            {
                if (!serDesComm.IsOpen)
                {
                    UIMessageDialog.ShowWarningDialog(FindForm(), "CH347通道未打开!!!");
                }
                else
                {
                    InitializeComponent();
                    TabControl scriptTC = new UITabControl();
                    scriptTC.Alignment = TabAlignment.Top;
                    this.Controls.Add(scriptTC);
                    scriptTC.Dock = DockStyle.Fill;
                    if (scriptTC.TabPages.Count == 0)
                    {
                        int maxScriptWinCnt = 5;

                        //scriptTC.Appearance = TabAppearance.FlatButtons;
                        //scriptTC.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;

                        for (int ix = 0; ix < maxScriptWinCnt; ix++)
                        {
                            FormScript frm_script = new FormScript(serDesComm);
                            frm_script.MeditorA = meditor;
                            scriptTC.TabPages.Add("Script" + ix.ToString());
                            scriptTC.TabPages[ix].Controls.Add(frm_script);
                            frm_script.Dock = DockStyle.Fill;
                            frm_script.BorderStyle = BorderStyle.Fixed3D;
                        }

                        //scriptTC.Alignment = TabAlignment.Bottom;
                    }
                }

            }
            else
            {
                UIMessageDialog.ShowWarningDialog(FindForm(), "未选择CH347通道!!!");
            }

        }
        public Script_Form(string name, AddressMeditor meditor,USBDeviceMeditor usbDeviceMeditor)
        {
            meditorA = meditor;
            this.usbDeviceMeditor = usbDeviceMeditor;
            serDesComm = InitSer(name);
            if (serDesComm == null)
            {
                //if (!serDesComm.IsOpen)
                //{
                //    UIMessageDialog.ShowWarningDialog(FindForm(), "CH347通道未打开!!!");
                //}
                //else
                //{
                //    InitializeComponent();
                //    scriptTC = new UITabControl();
                //    scriptTC.ContextMenuStrip = contextMenuStrip1;
                //    scriptTC.Alignment = TabAlignment.Top;
                //    this.Controls.Add(scriptTC);
                //    scriptTC.Dock = DockStyle.Fill;
                //    if (scriptTC.TabPages.Count == 0)
                //    {
                //        int maxScriptWinCnt = 5;

                //        //scriptTC.Appearance = TabAppearance.FlatButtons;
                //        //scriptTC.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                //        //
                //        for (int ix = 0; ix < maxScriptWinCnt; ix++)
                //        {
                //            FormScript frm_script = new FormScript(serDesComm);
                //            frm_script.MeditorA = meditor;
                //            frm_script.UsbDeviceMetor = usbDeviceMeditor;
                //            scriptTC.TabPages.Add("Script" + ix.ToString());
                //            scriptTC.TabPages[ix].Controls.Add(frm_script);
                //            frm_script.Dock = DockStyle.Fill;
                //            frm_script.BorderStyle = BorderStyle.Fixed3D;
                //        }

                //        //scriptTC.Alignment = TabAlignment.Bottom;
                //    }
                //}
                UIMessageDialog.ShowWarningDialog(FindForm(), "未选择CH347通道!!!");
                serDesComm = new SerDesComm();
                serDesComm.OpenDevice(0);
            }
            //else
            //{
            //    UIMessageDialog.ShowWarningDialog(FindForm(), "未选择CH347通道!!!");
            //}
            InitializeComponent();
            scriptTC = new UITabControl();
            scriptTC.ContextMenuStrip = contextMenuStrip1;
            scriptTC.Alignment = TabAlignment.Top;
            this.Controls.Add(scriptTC);
            scriptTC.Dock = DockStyle.Fill;
            if (scriptTC.TabPages.Count == 0)
            {
                int maxScriptWinCnt = 5;

                //scriptTC.Appearance = TabAppearance.FlatButtons;
                //scriptTC.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                //
                for (int ix = 0; ix < maxScriptWinCnt; ix++)
                {
                    FormScript frm_script = new FormScript(serDesComm);
                    frm_script.MeditorA = meditor;
                    frm_script.UsbDeviceMetor = usbDeviceMeditor;
                    scriptTC.TabPages.Add("Script" + ix.ToString());
                    scriptTC.TabPages[ix].Controls.Add(frm_script);
                    frm_script.Dock = DockStyle.Fill;
                    frm_script.BorderStyle = BorderStyle.Fixed3D;
                }

                //scriptTC.Alignment = TabAlignment.Bottom;
            }

        }

        private SerDesComm InitSer(string name)
        {
            if (MainForm.IniSerCommPairs.ContainsKey(name)) {
                return (MainForm.IniSerCommPairs[name]);
            }
            else
            {
                //UIMessageDialog.ShowErrorDialog(FindForm(),"请选择CH347通道");
                return null;
            }
            
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormScript frm_script = new FormScript(serDesComm);
            frm_script.MeditorA = meditorA;
            frm_script.MeditorA.NotifyAddrssChanged(meditorA.Addrss);
            frm_script.UsbDeviceMetor = usbDeviceMeditor;
            frm_script.UsbDeviceMetor.NotifySerDesChanged(usbDeviceMeditor.USBDevice);
            int ix = scriptTC.TabPages.Count;
            scriptTC.TabPages.Add("Script" + ix.ToString());
            scriptTC.TabPages[ix].Controls.Add(frm_script);
            frm_script.Dock = DockStyle.Fill;
            frm_script.BorderStyle = BorderStyle.Fixed3D;
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tab = scriptTC.TabPages[scriptTC.TabPages.Count - 1];
            scriptTC.TabPages.Remove(tab);
        }
    }

}
